

package com.mt.mall.controller.good;

import com.mt.common.core.web.JsonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.good.SpecificationEditDto;
import com.mt.mall.entity.good.Specification;
import com.mt.mall.service.good.SpecificationService;
import com.mt.mall.service.good.CommodityService;
import com.mt.mall.service.good.SpecificationService;
import com.mt.common.system.service.AttachmentService;


@Api(tags = "商品销售规格")
@RestController
@RequestMapping("/api/mall/good/Specification")
@CrossOrigin(allowCredentials = "true")
public class SpecificationController {
    private static Logger logger = LoggerFactory.getLogger(SpecificationController.class);


    @Autowired
    private SpecificationService specificationService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private AttachmentService attachmentService;

    /**
     * 根据分页参数查询商品销售规格集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("分页查询商品销售规格")
    @ApiPageParam
    @PostMapping("/findSpecifications")
    public PageResultDTO findSpecifications(@RequestBody PageDTO pageDTO) {
        return this.specificationService.findSpecifications(pageDTO);
    }

	@ApiOperation("查询所有商品销售规格")
	@ApiPageParam
	@GetMapping("/findAllSpecifications")
	public List<Specification> findAllSpecifications() {
		return this.specificationService.findAllSpecifications();
	}

    @ApiPageParam
    @PostMapping("/findSpecificationsByCommodityId")
    public List<Specification> findSpecificationsByCommodityId(@Param("commodityId")Long commodityId) {
        return this.specificationService.findSpecificationsByCommodityId(commodityId);
    }
    /**
     * 根据ID查询指定的商品销售规格
     *
     * @param specificationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("根据id查询商品销售规格")
    @ApiPageParam
    @PostMapping("/findSpecification")
    public Specification findSpecification(@RequestParam Long specificationId) {
        return this.specificationService.findSpecification(specificationId);
    }

    /**
     * 根据ID查询指定的商品销售规格(包含外键名称)
     *
     * @param specificationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(包含外键名称)")
    @PostMapping("/findSpecificationForView")
    public Specification findSpecificationForView(@RequestParam Long specificationId) {
        return this.specificationService.findSpecificationWithForeignName(specificationId);
    }

    /**
     * 根据ID查询指定的商品销售规格(包含商品销售规格和外键名称)
     *
     * @param specificationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(包含商品销售规格和外键名称)")
    @PostMapping("/findSpecificationForEdit")
    public SpecificationEditDto findSpecificationForEdit(@RequestParam Long specificationId) {
        SpecificationEditDto specificationEditDto = new SpecificationEditDto();
        specificationEditDto.setSpecification(this.specificationService.findSpecificationWithForeignName(specificationId));

        this.prepareSpecificationEditDto(specificationEditDto);

        return specificationEditDto;
    }

    /**
     * 根据ID查询指定的商品销售规格(只提取ID 和 Name)
     *
     * @param specificationId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("根据ID查询指定的商品销售规格(只提取ID 和 Name)")
    @PostMapping("/findSpecificationsWithIdNameById")
    public Specification findSpecificationsWithIdNameById(@RequestParam Long specificationId) {
        return this.specificationService.findSpecificationsWithIdNameById(specificationId);
    }

    /**
     * 根据名称查询商品销售规格集合(只提取ID 和 Name)
     *
     * @param specificationName 名称
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:view')")
    @ApiOperation("根据名称查询商品销售规格集合(只提取ID 和 Name)")
    @PostMapping("/findSpecificationsWithIdNameByName")
    public List<Specification> findSpecificationsWithIdNameByName(@RequestParam String specificationName) {
        return this.specificationService.findSpecificationsWithIdNameByName(specificationName);
    }


    /**
     * 创建新的商品销售规格
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:add')")
    @ApiOperation("创建新的商品销售规格")
    @PostMapping("/createSpecification")
    public SpecificationEditDto createSpecification() {
        SpecificationEditDto specificationEditDto = new SpecificationEditDto();
        specificationEditDto.setSpecification(new Specification());

        this.prepareSpecificationEditDto(specificationEditDto);
        return specificationEditDto;
    }

    private void prepareSpecificationEditDto(SpecificationEditDto specificationEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        specificationEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        specificationEditDto.setParentSpecifications(this.specificationService.findAllSpecificationsWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        specificationEditDto.setDescriptionImgAttachments(this.attachmentService.findAllAttachmentsWithIdName());
    }

    /**
     * 新增保存商品销售规格
     *
     * @param specification 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:add')")
    @ApiOperation("新增保存商品销售规格")
    @PostMapping("/saveSpecification")
    public Specification saveSpecification(@RequestBody Specification specification) {
        return this.specificationService.saveSpecification(specification);
    }

    /**
     * 修改保存商品销售规格
     *
     * @param specification 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:update')")
    @ApiOperation("修改保存商品销售规格")
    @PostMapping("/updateSpecification")
    public Specification updateSpecification(@RequestBody Specification specification) {
        return this.specificationService.updateSpecification(specification);
    }


    /**
     * 根据ID删除商品销售规格
     *
     * @param specificationId ID
     */
    @PreAuthorize("hasAuthority('mall:good:Specification:remove')")
    @ApiOperation("根据ID删除商品销售规格")
    @PostMapping("/deleteSpecification")
    public void deleteSpecification(@RequestParam Long specificationId) {
        this.specificationService.deleteSpecification(specificationId);
    }


}

